<template>
  <component :is="currentRouter">
  </component>
</template>
<script>
import Api from './api/home.js'
import { mapState } from 'vuex'
export default {
  name: 'Home',
  data() {
    return {
      flag: 0,
      routerDatas: [],//所有路由临时变量
      currentRouter: ''//当前角色获取的路由
    }
  },
  computed: {
    ...mapState({
      orgCode: (state) => state.login.currentOrgCode,
    }),
  },
  watch: {
    orgCode(v) {
      if (v) {
        this.getCompany()
        this.$store.commit('homePage/setFlag', true)
      }
    },
  },
  activated() {
    if (this.$store.state.homePage.flag) {
      this.getCompany()
    }
  },
  methods: {
    //获取所有路由,动态匹配设置的首页路由
    getComponents() {
      let routes = []
      this.routerDatas = window.app.routes
      this.routerDatas.forEach((element) => {
        if (element.component) {
          if (element.children) {
            element.children.forEach((el) => {
              el.newPath = `${element.path}/${el.path}`
            })
            routes.push(...element.children)
          }
        }
      })
      return routes
    },
    getInfo(code) {
      let _this = this
      Api.getInfo(code)
        .then((res) => {
          if (res.data.appFunction) {
            let router = this.getComponents().find(el => el.newPath === res.data.appFunction.action)
            this.currentRouter = router.component
          }/*  else {
              //没有设置首页默认跳转预警分析首页
            let router = this.getComponents().find(el=>el.newPath ==='/analysis/indexs/page')
            this.currentRouter = router.component
          } */
        })
        .catch((error) => {
          this.notify(error)
        })
    },
    getCompany() {
      Api.getCompany()
        .then((res) => {
          this.getInfo(res.data.orgCode)
        })
        .catch((error) => {
          this.notify(error)
        })
    },
  },
}
</script>